﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Jobping.StickyBeak.UI
{
	public class HomeHtml
	{
		public HomeHtml(HomeVD vd)
		{
			this.VD = vd;
		}

		public HomeVD VD { get; set; }

		public override string ToString()
		{
			StringBuilder sb = new StringBuilder();

			sb.Append(HtmlConstants.Header);

			if (VD.Pages != null)
				BuildForm(sb);

			if (VD.Results != null)
				BuildTable(sb);
			else
				sb.Append("<div class='empty'>No Results</div>");

		
			sb.Append(HtmlConstants.Footer);

			return sb.ToString();
		}

		private void BuildForm(StringBuilder sb)
		{
			sb.Append("<form>");
			sb.Append(@"<div id='info'><strong>Welcome</strong>, click on any request below to see the full details</div>");
			sb.Append("Select a log file to view: <select name='pageName'>");
			foreach (var item in VD.Pages)
			{
				sb.option(item, VD.SelectedPage);
			}
			sb.Append("</select>&nbsp;<input type='submit' value='Select Page' />");

			sb.Append("<br />Stickey is <strong>" + (this.VD.Enabled ? "On" : "Off") + "</strong>&nbsp;<input type='submit' name='toggle' value='Turn " + (this.VD.Enabled ? "Off" : "On") + "' title='This will not affect the default startup state (on/off) of stickybeak, the startup state is controlled by the configuration file' />");

			sb.Append("</form>");

		}

		private void BuildTable(StringBuilder sb)
		{
			sb.Append("<table class='logResults'>");

			sb.Append("<tr>");
			sb.th("Date");
			sb.th("Method");
			sb.th("Url");
			sb.th("Username");
			sb.th("IPAddress");
			sb.th("BrowserId");
			sb.th("");
			sb.Append("</tr>");
			sb.Append("<tbody>");
			foreach (var log in this.VD.Results)
			{
				sb.Append("<tr>");

				sb.td(log.DateUtc.ToLocalTime().ToString());
				sb.td(log.HttpMethod);
				sb.td(log.Url);
				sb.td(log.Username);
				sb.td(log.IPAddress);
				sb.td(log.BrowserId);

				sb.Append("<td class='last'><div class='hover'><div class='target'>");

				sb.h6("Headers");

				if (log.HeaderValues.Length > 0)
					sb.table(log.HeaderValues);
				else
					sb.Append("<div class='empty'>N/A</div>");

				sb.h6("QueryString");

				if (log.QueryStringValues.Length > 0)
					sb.table(log.QueryStringValues);
				else
					sb.Append("<div class='empty'>N/A</div>");

				sb.h6("Form");

				if (log.FormValues.Length > 0)
					sb.table(log.FormValues);
				else
					sb.Append("<div class='empty'>N/A</div>");

				sb.h6("Cookies");
				if (log.CookieValues.Length > 0)
					sb.table(log.CookieValues);
				else
					sb.Append("<div class='empty'>N/A</div>");

				sb.h6("Session");
				if (log.SessionValues.Length > 0)
					sb.table(log.SessionValues);
				else
					sb.Append("<div class='empty'>N/A</div>");

				sb.Append("</div></div></td>");

				sb.AppendLine("</tr>");
			}
			sb.Append("</tbody>");
			sb.Append("</table>");
		}


	}
	public static class HtmlExtensions
	{
		public static void h6(this StringBuilder sb, string content)
		{
			sb.Append("<h6>");
			sb.Append(content);
			sb.Append("</h6>");
		}

		public static void th(this StringBuilder sb, string content)
		{
			sb.Append("<th>");
			sb.Append(content);
			sb.Append("</th>");
		}
		public static void td(this StringBuilder sb, string content)
		{
			sb.Append("<td>");
			sb.Append(content);
			sb.Append("</td>");
		}

		public static void table(this StringBuilder sb, RequestLogValue[] items)
		{
			sb.Append("<table>");
			foreach (var item in items)
			{
				sb.Append("<tr>");
				sb.th(item.Key);
				sb.td(item.Value);
				sb.AppendLine("</tr>");
			}
			sb.Append("</table>");
		}

		public static void option(this StringBuilder sb, string content, string value)
		{
			if (String.Compare(content, value, true) != 0)
				sb.Append("<option>");
			else
				sb.Append("<option selected='selected'>");

			sb.Append(content);
			sb.Append("</option>");
		}
	}
}
